/********************************************************************
	created:	2011/05/14
	created:	14:5:2011   11:30
	filename: 	d:\Work\Job\trunk\Speed\Tracker.h
	file path:	d:\Work\Job\trunk\Speed
	file base:	Tracker
	file ext:	h
	author:		liang
	
	purpose:	
*********************************************************************/
#ifndef _Tracker_h_2011_05_14_11_30
#define _Tracker_h_2011_05_14_11_30

#include "MetaFileParser.h"

struct Peer_Addr
{
	char ip[6];
	unsigned char port;
	Peer_Addr* pNext;
};


class Tracker
{
public:
	Tracker();
	virtual ~Tracker();

	static int HttpEncode(unsigned char* pIn,int InLen,unsigned char* pOut,int OutLen);

	static int GetTrackerName(Announce_list* pNode,char* pName,int Len);

	static int GetTrackerPort(Announce_list* pNode,unsigned short* pPort);

	static int CreateRequest(char* pRequest,int len,Announce_list* pNode,unsigned short port,long long Down,long long Up,long long Left,int NumWant);

	int PrepareConnectTracker(int* pMaxSockFd);

	int PrepareConnectPeer(int* pMaxSockFd);

	int GetReponseType(char* pBuffer,int Len,int* pTotalLength);

	int ParseTrackerResponse1(char* pBuffer,int ret,char* pRedirection,int len);

	int ParseTrackerResponse2(char* pBuffer,int ret);

	int AddPeerNodeToPeerList(int* pSock,sockaddr_in saptr);

	void FreePeerAddrHead();

	void ReleaseMemoryInTracker();
};


#endif